package com.mdl.springmvc.handler.service.impl;

import com.mdl.springmvc.handler.common.GLConstants;
import com.mdl.springmvc.handler.dao.GailouMapper;
import com.mdl.springmvc.handler.entity.Gailou;
import com.mdl.springmvc.handler.service.GaiLouService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Desc
 * @Author madengling
 * @Time 2020/11/4 14:07
 */
@Service
public class GaiLouServiceImpl implements GaiLouService {
    private static Logger logger = Logger.getLogger(GaiLouServiceImpl.class);
    @Autowired
    private GailouMapper gailouMapper;

    @Override
    public Gailou getGaiLouById(Integer id){
        if(id!=null){
            Gailou gailou = gailouMapper.selectByPrimaryKey(id);
            return gailou;
        }
        return null;
    }

    @Override
    public List<Gailou> getGaiLouAll() {
        return gailouMapper.selectAll();
    }

    @Override
    public void putYuLiuMessage(Gailou gailou, String yuliuMessage) {
        logger.info("putYuLiuMessage...");
        if(StringUtils.isEmpty(gailou.getZhuli1()) || gailou.getZhuli1()==null){
            gailou.setZhuli1(yuliuMessage);
        }else if(StringUtils.isEmpty(gailou.getZhuli2()) || gailou.getZhuli2()==null){
            gailou.setZhuli2(yuliuMessage);
        }else if(StringUtils.isEmpty(gailou.getZhuli3()) || gailou.getZhuli3()==null){
            gailou.setZhuli3(yuliuMessage);
        }else if(StringUtils.isEmpty(gailou.getZhuli4()) || gailou.getZhuli4()==null){
            gailou.setZhuli4(yuliuMessage);
        }else if(StringUtils.isEmpty(gailou.getZhuli5()) || gailou.getZhuli5()==null){
            gailou.setZhuli5(yuliuMessage);
        }else if(StringUtils.isEmpty(gailou.getZhuli6()) || gailou.getZhuli6()==null){
            gailou.setZhuli6(yuliuMessage);
        }else {
            logger.error("助力次数不足");
            throw new RuntimeException("助力次数不足");
        }
        gailouMapper.updateByPrimaryKey(gailou);
    }

    @Override
    public void putZhuliJian(Gailou gailou) {
        logger.info("putZhuliJian...");
        if(GLConstants.ZHUILI.equals(gailou.getZhuli6())){
            gailou.setZhuli6("");
        }else if(GLConstants.ZHUILI.equals(gailou.getZhuli5())){
            gailou.setZhuli5("");
        }else if(GLConstants.ZHUILI.equals(gailou.getZhuli4())){
            gailou.setZhuli4("");
        }else if(GLConstants.ZHUILI.equals(gailou.getZhuli3())){
            gailou.setZhuli3("");
        }else if(GLConstants.ZHUILI.equals(gailou.getZhuli2())){
            gailou.setZhuli2("");
        }else if(GLConstants.ZHUILI.equals(gailou.getZhuli1())){
            gailou.setZhuli1("");
        }else {
            logger.error("异常，请联系管理员检查");
            throw new RuntimeException("异常，请联系管理员检查");
        }
        gailouMapper.updateByPrimaryKey(gailou);
    }

    @Override
    public void editYuLiuMessage(Gailou gailou, String dengji, Integer cishu, String yuliuMessage) {
        logger.info("editYuLiuMessage...");
        if(dengji!=null){
            gailou.setDengji(dengji);
        }
        if(GLConstants.ZHULI1.equals(cishu)){
            gailou.setZhuli1(yuliuMessage);
        }else if(GLConstants.ZHULI2.equals(cishu)){
            gailou.setZhuli2(yuliuMessage);
        }else if(GLConstants.ZHULI3.equals(cishu)){
            gailou.setZhuli3(yuliuMessage);
        }else if(GLConstants.ZHULI4.equals(cishu)){
            gailou.setZhuli4(yuliuMessage);
        }else if(GLConstants.ZHULI5.equals(cishu)){
            gailou.setZhuli5(yuliuMessage);
        }else if(GLConstants.ZHULI6.equals(cishu)){
            gailou.setZhuli6(yuliuMessage);
        }else {
            logger.error("异常，请联系管理员检查");
            throw new RuntimeException("异常，请联系管理员检查");
        }
        gailouMapper.updateByPrimaryKey(gailou);
    }

    @Override
    public void qingchuZhuliJian(Gailou gailou) {
        logger.info("qingchuZhuliJian...");
        gailou.setZhuli1("");
        gailou.setZhuli2("");
        gailou.setZhuli3("");
        gailou.setZhuli4("");
        gailou.setZhuli5("");
        gailou.setZhuli6("");
        gailouMapper.updateByPrimaryKey(gailou);
    }
}
